<?php

namespace app\api\controller\clean;

use app\common\controller\Api;
use app\common\model\Order as ModelOrder;
use app\common\model\order\Code;

/**
 * 清洗点订单接口
 */
class Order extends Api
{

    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = [];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    /**
     * 列表
     *
     */
    public function list()
    {
        $get =  request()->get();
        extract($get);
        if (!isset($type) || empty($type)) {
            $this->error('参数错误');
        }
        $order = new ModelOrder();
        if ($type != 3) {
            $order->whereIn('order.clean', $this->auth->id);
        }
        $list = $order->with(['goods', 'address', 'shop', 'pickInfo', 'cleanInfo', 'retrieveInfo'])->where('order.status', $type)->order('createtime desc')->paginate(10);
        $this->success('返回成功', $list);
    }


    /**
     * 确认入库
     *
     */
    public function receipt()
    {
        $post =  request()->post();
        extract($post);
        if (!isset($id) || empty($id)) {
            $this->error('参数错误');
        }
        $order = new ModelOrder();
        $order = $order->get($id);
        if ($order['status'] != 3) {
            $this->error('订单状态错误');
        }
        $code = new Code;
        $date = date('ym');
        $id = $code->whereTime('createtime', 'year')->where('prefix', $date)->max('id');
        $num = 1;
        if ($id) {
            $num = intval($id++);
        }
        $len = 4;
        if (strlen((string) $num) > 4) {
            $len = strlen((string) $num);
        }
        $order_code = str_pad($num, $len, '0', STR_PAD_LEFT);
        $order_code = $date . $order_code;
        $data['prefix'] = $date;
        $data['qh_code'] = $order_code;
        $data['order_id'] = $order['id'];
        $code = $code->allowField(true)->save($data);
        $res = $order->save(['status' => '4', 'clean' => $this->auth->id, 'cleantime' => time(), 'qh_image' => build($id), 'qh_code' => $order_code,'text'=>$text]);
        if ($res) {
            $this->success('取货成功',$order_code);
        }
        $this->error('取货失败');
    }
    /**
     * 确认出库
     *
     */
    public function delivery()
    {
        $post =  request()->post();
        extract($post);
        if (!isset($id) || empty($id)) {
            $this->error('参数错误');
        }
        $order = new ModelOrder();
        $order = $order->get($id);
        if ($order['status'] != 4) {
            $this->error('订单状态错误');
        }
        $res = $order->save(['status' => '5']);
        if ($res) {
            $this->success('确认成功');
        }
        $this->error('确认失败');
    }



    /**
     * 清洗点数据统计
     *
     */
    public function sao_pickings()
    {
        $data['today_num'] = ModelOrder::where('clean', $this->auth->id)->whereTime('cleantime', 'today')->where('status', '<>', '-1')->count();
        $data['month_num'] = ModelOrder::where('clean', $this->auth->id)->whereTime('cleantime', 'month')->where('status', '<>', '-1')->count();
        $this->success('', $data);
    }
}
